package com.study.graph.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import static java.lang.Math.abs;

@Slf4j
public class TimeUtil {
    private static  final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    public static String getCurrentTime(){
        return simpleDateFormat.format(new Date());
    }

    private static SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmss");

    public static int days_compare(String sd1, String sd2) {
        int d = 0;
        try {
            long ld1 = sd.parse(sd1).getTime();
            long ld2 = sd.parse(sd2).getTime();
            d = days_compare(ld1,ld2);
        } catch (ParseException e) {
            log.error("data1 :[{}], data2 :[{}]",sd1, sd2);
            e.printStackTrace();
        }

        return  d;
    }
    public static int days_compare(long ld1, long ld2) {
        Date date1= new Date(ld1);
        Date date2= new Date(ld2);
        return days_compare(date1,date2);
    }

    public static int days_compare(Date date1, Date date2) {
        Calendar d1 = new GregorianCalendar();
        d1.setTime(date1);
        Calendar d2 = new GregorianCalendar();
        d2.setTime(date2);
        if(date1.getTime()==date2.getTime())
            return 0;
        else {
            int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
            return abs(days) + 1;
        }
    }

    public static void main(String[] args) {
        Date d1 = new Date();
        Date d2 = new Date(d1.getTime()+12930);
        int days = days_compare(d2,d1);
        log.info("days : {}",days);
        log.info("days : {}",days_compare(d1,d1));
        log.info("days : {}",days_compare(123321,123321));
        log.info("days : {}",days_compare(123321,123322));
        //20210814101849_20210817154622_981579_1015966
        log.info("days : {}",days_compare("20210814101849","20210814101849"));
        log.info("days : {}",days_compare("20210814101849","20210817154622"));


    }
}
